﻿/*  Windows Powerplan Switcher
    Copyright (C) 2011 Tom Schreiber / Codebit
 
    http://codebit.de/windows-power-plan-switcher
    mailto: tom.schreiber@codebit.de

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Codebit.PowerPlanSwitcher
{
    public partial class SwitcherNotifyIcon
    {
        // NotifyIcon
        System.ComponentModel.Container components = null;
        System.Windows.Forms.NotifyIcon notifyIcon;

        // Context-Menu
        private ContextMenu contextMenu;

        // PowerPlan-Handler
        WinPowerPlanHandler handler;

        // Initalize
        public SwitcherNotifyIcon()
        {
            this.components = new System.ComponentModel.Container();
            this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);

            // Initialize Notify-Icon    
            notifyIcon.Text = "Windows Power Plan Switcher";
            notifyIcon.Icon = PowerPlanSwitcher.Resources.icon16;
            notifyIcon.Visible = true;

            // Initialize Handler
            handler = new WinPowerPlanHandler();

            // Create Context-Menu
            CreateMenu();
        }

        // Creates/Updates the Menu
        private void CreateMenu()
        {
            // Initialize contextMenu
            contextMenu = new ContextMenu();

            // Get Power-Schemes
            foreach (PowerScheme ps in handler.PowerSchemes)
            {
                MenuItem mi = new MenuItem();
                mi.Text = ps.Name;
                mi.Checked = ps.Active;
                mi.Tag = ps.GUID;
                mi.Click += new System.EventHandler(this.ActivatePowerplan);
                contextMenu.MenuItems.Add(mi);
            }

            // Add separator
            MenuItem miSep1 = new MenuItem();
            miSep1.Text = "-";
            contextMenu.MenuItems.Add(miSep1);

            // InfoBox
            MenuItem miInfo = new MenuItem();
            miInfo.Text = "Info...";
            contextMenu.MenuItems.Add(miInfo);
            miInfo.Click += new System.EventHandler(this.OpenInfoBox);

            // Add separator
            MenuItem miSep2 = new MenuItem();
            miSep2.Text = "-";
            contextMenu.MenuItems.Add(miSep2);

            // Initialize/Add "Exit" - MenuItem
            MenuItem miExit = new MenuItem();
            miExit.Text = "E&xit";
            miExit.Click += new System.EventHandler(this.Exit);
            contextMenu.MenuItems.Add(miExit);

            // Add to Notify-Icon
            notifyIcon.ContextMenu = contextMenu;
        }

        // Activate a specific powerplan
        private void ActivatePowerplan(object Sender, EventArgs e)
        {
            // get guid of the menu-item thats was clickes
            MenuItem m = (MenuItem)Sender;
            string guid = (string)m.Tag;

            // switch to power-scheme
            handler.SwitchToScheme(guid);

            // update menu
            CreateMenu();
        }

        /// <summary>
        /// Opens About/Info box
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="e"></param>
        private void OpenInfoBox(object Sender, EventArgs e)
        {
            InfoBox box = new InfoBox();
            box.Show();
        }

        // Exit Program
        private void Exit(object Sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            Application.Exit();
        }

    }
}
